import json
import time

from selenium import webdriver

driver = webdriver.Chrome()


def save(obj, path):
    """
    将对象存入json文件
    :param obj:
    :return:
    """
    with open(path, 'w', encoding="UTF-8") as f:
        json.dump(obj, f, ensure_ascii=False)


def get_and_save_cookies():
    """
    获取浏览器当前的cookie
    :return:
    """
    save(driver.get_cookies(), './cookies.json')


def close_chrome():
    driver.close()


def open_chrome(cookies, url):
    """
    打开网页
    :param cookies:
    :return:
    """
    driver.get(url)
    for cookie in cookies:
        driver.add_cookie(cookie)
    driver.get(url)
    driver.implicitly_wait(1)
    driver.maximize_window()


if __name__ == '__main__':
    driver.get("https://twitter.com/")
    time.sleep(200)
    get_and_save_cookies()
